package ace.module.stock.core.impl.dao.model.bo;

import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.model.SaveContext;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.model.SaveListContext;
import ace.cmp.data.spring.data.jdbc.querydsl.rsql.core.utils.SaveContextUtils;
import ace.module.stock.core.impl.dao.entity.WmsSalesQuantity;
import ace.module.stock.core.impl.dao.entity.WmsStock;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;

/**
 * 库存.
 *
 * @author Caspar
 */
@Data
@SuperBuilder
@NoArgsConstructor
@AllArgsConstructor
public class WmsSalesQuantityDetailSaveContextBo implements Serializable {

  private SaveContext<WmsSalesQuantity> salesQuantity;

  private SaveListContext<WmsSalesQuantity> children;

  public SaveListContext<WmsSalesQuantity> getSaveListContext() {
    List<SaveContext<WmsSalesQuantity>> saveContexts = new ArrayList<>(children.getData());
    saveContexts.add(salesQuantity);
    return SaveContextUtils.of(saveContexts);
  }

  public List<Long> getAllIds() {
    List<Long> result = this.getChildrenIds();
    result.add(salesQuantity.getData().getId());
    return result;
  }

  public List<Long> getChildrenIds() {
    return Optional.ofNullable(children)
        .map(SaveListContext::getOriginData)
        .stream()
        .flatMap(List::stream)
        .map(WmsSalesQuantity::getId)
        .collect(Collectors.toList());
  }
}
